package com.athuahua.spzx.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * @Author: huahua
 * @Date: 2024/5/13 0013
 * @Time: 12:06
 * @Description: Redis key序列化器
 */

@Configuration
@Slf4j
public class RedisConfiguration {
    //springboot3.0以后，默认使用Lettuce作为redis客户端，不需要再配置RedisTemplate
    //但是! springboot3的redis没有key的序列化器，就会导致一些问题，例如redistemplate.keys(xxx) 获取不到东西
    //配置redisTemplate

    @Bean
    public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        log.info("开始创建redis模板对象");
        RedisTemplate<String, String> redisTemplate = new StringRedisTemplate();
        //设置连接工厂对象
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        //自定义redis key的序列化方式
        redisTemplate.setKeySerializer(new StringRedisSerializer());


        return redisTemplate;
    }



}
